<template>
  <div v-if="iswhOK" class="login-container"
       :style="{ width: backgroundWidth + 'px', height: backgroundHeight + 'px' }">
    <!-- 背景动画 -->
    <div class="background"></div>
    <!-- 主要内容区域 -->
    <div class="content-wrapper">
      <!-- 欢迎信息 -->
      <div class="welcome-section">
        <h1 class="welcome-title">欢迎访问信息工程系平台</h1>
        <p class="welcome-desc">一站式数字服务平台</p>
      </div>
      <!-- 登录表单与快捷导航 -->
      <div class="main-content">
        <!-- 登录表单 -->
        <div class="login-card">
            <LoginForm/>
            <div class="login-footer">
              <a href="javascript:" @click="RegisterdialogVisible" class="text-link">没有账号？立即注册</a>
            </div>
        </div>

        <!-- 快捷导航 -->
        <div class="quick-nav">
          <h3 class="nav-title">常用服务</h3>
          <div class="nav-grid">
            <a v-for="site in quickSites" :key="site.name" :href="site.url" target="_blank" class="nav-card">
                <component :is="site.icon"/>
              <span class="card-name">{{ site.name }}</span>
            </a>
          </div>
        </div>
      </div>
    </div>

    <!-- 注册对话框 -->
    <el-dialog
        title="注册"
        v-model="dialogVisible"
        :style="{ width: 540 + 'px' }"
        @close="dialogVisible = false"
    >
      <RegisterConfirmForm v-if="RegisterConfirmVisible" @register-confirm-event="handleRegisterConfirm"/>
      <RegistrationForm v-if="RegistrationFormVisible" @register="handleRegister"/>
    </el-dialog>
  </div>
</template>

<script setup>
import {ref, onMounted, onBeforeUnmount} from 'vue';
import LoginForm from '@/components/LoginForm.vue';
import RegistrationForm from '@/components/RegistrationForm.vue';
import RegisterConfirmForm from "@/components/RegisterConfirmForm.vue";
import router from "@/router/index.js";
import {ElDialog, ElMessage} from 'element-plus';
// 响应式数据
const dialogVisible = ref(false);
const backgroundWidth = ref(0);
const backgroundHeight = ref(0);
const RegistrationFormVisible = ref(false);
const RegisterConfirmVisible = ref(true);
const iswhOK = ref(false);

// 快捷网站列表
// 快捷网站列表（修正了 `Documentation` → `Document`）
const quickSites = [
  { name: '教务系统', url: 'http://119.6.110.75:9106/', icon: 'Document' }, // ✅ 修正
  { name: '图书馆', url: 'https://www.swjtuhc.cn/html/tsg/index', icon: 'Reading' },
  { name: '系部门户', url: 'https://www.swjtuhc.cn/html/xxgc/', icon: 'OfficeBuilding' },
  { name: '学生平台', url: '/#/student', icon: 'User' },
  { name: '毕设知网', url: 'https://co2.cnki.net/Login.html?dp=swjtuhc&r=1746601472441', icon: 'Memo' },
  { name: '校友邦实习', url: 'https://m.xybsyw.com/sub/#/', icon: 'Suitcase' },
  { name: '学习通', url: 'https://v8.chaoxing.com/', icon: 'Monitor' },
  { name: '内网下载站', url: 'https://nextcloud.swjtuhc.top:1443/s/wjSKKxmiK6s9iGZ', icon: 'Download' }
]

// 生命周期钩子
onMounted(() => {
  updateHeaderWidth();
  window.addEventListener('resize', updateHeaderWidth);
});

onBeforeUnmount(() => {
  window.removeEventListener('resize', updateHeaderWidth);
});

// 方法定义
function updateHeaderWidth() {
  backgroundHeight.value = window.innerHeight;
  backgroundWidth.value = backgroundHeight.value * (16 / 9);

  if (backgroundWidth.value < window.innerWidth - 10) {
    backgroundWidth.value = window.innerWidth;
    backgroundHeight.value = backgroundWidth.value * (9 / 16);
  }

  iswhOK.value = true;
}

const handleRegister = async (registrationData) => {
  try {
    const response = await global.jsonApi('/user/register/', JSON.stringify(registrationData));
    const data = await response.json();

    if (response.ok) {
      ElMessage.success('注册成功!');
    } else {
      ElMessage.error('注册失败: ' + data.error);
    }
  } catch (error) {
    ElMessage({
      message: error.message,
      type: 'error',
      duration: 3000
    });
  }
};

const handleRegisterConfirm = (confirmData) => {
  if (confirmData) {
    RegistrationFormVisible.value = true;
    RegisterConfirmVisible.value = false;
  }
};

const RegisterdialogVisible = () => {
  dialogVisible.value = true;
  RegistrationFormVisible.value = false;
  RegisterConfirmVisible.value = true;
};

const GoStudent = () => {
  router.push('/student');
};
</script>

<style scoped>
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;500;600&display=swap');

.login-container {
  position: relative;
  overflow: hidden;
  font-family: 'Poppins', sans-serif;
}

.background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(45deg, #ee7752, #ddc7ce, #23a6d5, #23d5ab);
  background-size: 400% 400%;
  animation: gradientAnimation 15s ease infinite;
  z-index: 0;
}

.content-wrapper {
  position: relative;
  z-index: 1;
  padding: 40px;
  max-width: 1200px;
  margin: 0 auto;
}

.welcome-section {
  text-align: center;
  color: white;
  margin-bottom: 40px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.welcome-title {
  font-size: 2.5rem;
  margin-bottom: 15px;
}

.welcome-desc {
  font-size: 1.2rem;
  opacity: 0.9;
}

.main-content {
  display: flex;
  justify-content: space-between;
  gap: 40px;
  flex-wrap: wrap;
}

.login-card {
  display: flex;
  flex-direction: column; /* 子元素垂直排列 */
  justify-content: center; /* 垂直居中 */
  align-items: center; /* 水平居中 */
  background: rgba(255, 255, 255, 0.1);
  border-radius: 15px;
  padding: 30px;
  width: 360px;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
}

.login-footer {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
  font-size: 0.9rem;
}

.text-link {
  color: #ffffffcc;
  text-decoration: none;
  transition: color 0.3s;
}

.text-link:hover {
  color: white;
}

.quick-nav {
  flex: 1;
  min-width: 300px;
}

.nav-title {
  color: white;
  margin-bottom: 20px;
  font-size: 1.5rem;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

.nav-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 20px;
}

.nav-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  color: white;
  text-decoration: none;
  transition: all 0.3s ease;
  backdrop-filter: blur(5px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.nav-card:hover {
  transform: translateY(-5px);
  background: rgba(255, 255, 255, 0.15);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
}

.card-icon {
  color: #000000; /* 使用黑色 */
}

.card-name {
  font-size: 1rem;
  text-align: center;
}

@keyframes gradientAnimation {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .main-content {
    flex-direction: column;
    align-items: center;
  }

  .login-card {
    width: 100%;
  }
}
</style>